home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / BASCNVRT.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  1KB  |  50 lines

  1. /*
  2. **  BASCNVRT.C - Convert between number bases
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdlib.h>
  8. #ifdef TEST
  9.  #include <stdio.h>
  10. #endif
  11.  
  12. /*
  13. **  Calling parameters: 1 - Number string to be converted
  14. **                      2 - Buffer for the  converted output
  15. **                      3 - Radix (base) of the input
  16. **                      4 - Radix of the output
  17. **
  18. **  Returns: Pointer to converted output
  19. */
  20.  
  21. char *base_convert(const char *in, char *out, int rin, int rout)
  22. {
  23.       long n;
  24.       char *dummy;
  25.  
  26.       n = strtol(in, &dummy, rin);
  27.       return ltoa(n, out, rout);
  28. }
  29.  
  30. #ifdef TEST
  31.  
  32. int main(int argc, char *argv[])
  33. {
  34.       int rin, rout;
  35.       char buf[40];
  36.  
  37.       if (4 > argc)
  38.       {
  39.             puts("Usage: BASCNVRT <number> <base_in> <base_out>");
  40.             return(-1);
  41.       }
  42.       rin  = atoi(argv[2]);
  43.       rout = atoi(argv[3]);
  44.       printf("%s (base %d) = %s (base %d)\n", argv[1], rin,
  45.             base_convert((const char *)argv[1], buf, rin, rout), rout);
  46.       return 0;
  47. }
  48.  
  49. #endif
  50.